package com.slipper.system.codeless.service;

import java.util.Collections;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.util.Tool;
import com.slipper.util.UUID;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.PrivilegeConditionDao;
import com.slipper.system.codeless.bo.PrivilegeConditionBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;

@Service
@ReadTransactional
public class PrivilegeConditionService {
    @Resource
    private PrivilegeConditionDao privilegeConditionDao;
    
    public PrivilegeConditionBO queryBy(String id) {
        return privilegeConditionDao.queryBy(id);
    }
    
    public List<PrivilegeConditionBO> queryList(PrivilegeConditionBO bo) {
        return privilegeConditionDao.queryList(bo);
    }
    
    public PageInfo<PrivilegeConditionBO> queryPage(PrivilegeConditionBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(privilegeConditionDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(String privilegeId, List<PrivilegeConditionBO> itemList, String employeeId) {
        if (Tool.isBlank(privilegeId) || itemList.isEmpty()) {
            return 0;
        }

        int i = 0;
        for (PrivilegeConditionBO item : itemList) {
            item.setOrdinal(++i);
            item.setPrivilegeId(privilegeId);
            if (Tool.isBlank(item.getId())) {
                item.setId(UUID.getUUID());
                item.setUpdateId(employeeId);
                item.setCreateId(employeeId);
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                privilegeConditionDao.insert(item);
            } else {
                item.setUpdateId(employeeId);
                item.setUpdateTime(System.currentTimeMillis());
                privilegeConditionDao.update(item);
            }
        }

        PrivilegeConditionBO qo = new PrivilegeConditionBO();
        qo.setPrivilegeId(privilegeId);
        privilegeConditionDao.queryList(qo)
                .stream().filter(condition -> itemList.stream().noneMatch(temp -> condition.getId().equals(temp.getId())))
                .forEach(this::delete);
        return 1;
    }

    @WriteTransactional
    public void saveList(List<PrivilegeConditionBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (privilegeConditionDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                privilegeConditionDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                privilegeConditionDao.insert(item);
            }
        });
    }

    @WriteTransactional
    public void copyByPrivilegeId(String oldPrivilegeId, String newPrivilegeId, String employeeId) {
        if (Tool.isBlank(oldPrivilegeId)) {
            return;
        }

        PrivilegeConditionBO qo = new PrivilegeConditionBO();
        qo.setPrivilegeId(oldPrivilegeId);
        privilegeConditionDao.queryList(qo).forEach(item -> {
            item.setId(UUID.getUUID());
            item.setPrivilegeId(newPrivilegeId);
            item.setUpdateId(employeeId);
            item.setCreateId(employeeId);
            item.setUpdateTime(System.currentTimeMillis());
            item.setCreateTime(System.currentTimeMillis());
            privilegeConditionDao.insert(item);
        });
    }

    @WriteTransactional
    public void deleteByPrivilegeId(String privilegeId) {
        if (Tool.isBlank(privilegeId)) {
            return;
        }

        privilegeConditionDao.deleteByPrivilegeId(privilegeId);
    }

    public List<PrivilegeConditionBO> queryByPrivilegeId(String privilegeId) {
        if (Tool.isBlank(privilegeId)) {
            return Collections.emptyList();
        }

        PrivilegeConditionBO qo = new PrivilegeConditionBO();
        qo.setPrivilegeId(privilegeId);
        return privilegeConditionDao.queryList(qo);
    }

    @WriteTransactional
    public void delete(PrivilegeConditionBO bo) {
        if (Tool.isBlank(bo.getId())) {
            return;
        }

        privilegeConditionDao.delete(bo);
    }
}
